﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using Infragistics.Win;
using Infragistics.Win.UltraWinGrid;

namespace CKNetFrameWork
{
    public partial class Frm_ClassTable : DockContent
    {
        private TrainingDB.BLL.CK_SchdDetail m_bllCK_SchdDetail = new TrainingDB.BLL.CK_SchdDetail();
        private TrainingDB.BLL.CK_TimeSegTab m_bllCK_TimeSegTab = new TrainingDB.BLL.CK_TimeSegTab();

        public Frm_ClassTable()
        {
            InitializeComponent();
        }
        /// 取得某月的第一天
        /// </summary>
        /// <param name="datetime">要取得月份第一天的时间</param>
        /// <returns></returns>
        private DateTime FirstDayOfWeek(DateTime dt)
        {
            DateTime startWeek;
            if(dt.DayOfWeek != DayOfWeek.Sunday)
                startWeek = dt.AddDays(1 - Convert.ToInt32(dt.DayOfWeek.ToString("d")));  //本周周一
            else
                startWeek = dt.AddDays(1 - Convert.ToInt32(dt.DayOfWeek.ToString("d")) - 7);

            return startWeek;
        }
        /// <summary>
        /// 取得某月的最后一天
        /// </summary>
        /// <param name="datetime">要取得月份最后一天的时间</param>
        /// <returns></returns>
        private DateTime LastDayOfWeek(DateTime dt)
        {
            DateTime startWeek;
            if (dt.DayOfWeek != DayOfWeek.Sunday)
                startWeek = dt.AddDays(1 - Convert.ToInt32(dt.DayOfWeek.ToString("d")));  //本周周一
            else
                startWeek = dt.AddDays(1 - Convert.ToInt32(dt.DayOfWeek.ToString("d")) - 7);

            DateTime endWeek = startWeek.AddDays(6);  //本周周日
            return endWeek;
        }

        private void Frm_ClassTable_Load(object sender, EventArgs e)
        {
            ShowClassTable(this.dateTimePickerRef.Value.Date);
        }

        private char Num2Upper(int nNum)
        {
            char cRet = new char();
            switch (nNum)
            {
                case 1:
            
                    {
                        cRet = '一';
                        break;
                    }
                case 2:
          
                    {
                        cRet = '二';
                        break;
                    }
                case 3:
           
                    {
                        cRet = '三';
                        break;
                    }
                case 4:
           
                    {
                        cRet = '四';
                        break;
                    }
                case 5:
        
                    {
                        cRet = '五';
                        break;
                    }
                case 6:
       
                    {
                        cRet = '六';
                        break;
                    }
                case 7:
                    {
                        cRet = '日';
                        break;
                    }
                default:
                    break;
            }

            return cRet;
        }
        private string ConvertGuiNoString(string strSource)
        {
            string str = strSource.ToString();
            string[] arr = str.Split(',');
            string tempa = "";
            int n = 0;
            foreach (string ch in arr)
            {

                if (ch != "")
                {
                    if (n == 0)
                        tempa += ch;
                    else
                        tempa += "\n" + ch;
                }

                n++;
            }

            return tempa;
        }
        private void ShowClassTable(DateTime dtRef)
        {
            DateTime startWeek = FirstDayOfWeek(dtRef);
            DateTime endWeek = LastDayOfWeek(dtRef);
            DataSet dsTemp = m_bllCK_SchdDetail.GetClassTalbe("classDate >= '" + startWeek + "'"
                + " and classDate <= '" + endWeek + "'");

            DataTable dt = new DataTable();
            int nIndex = 0;
            dt.Columns.Add("   ", System.Type.GetType("System.String"));
            dt.Columns["   "].SetOrdinal(nIndex++);

            int n = 1;
            List<DateTime> lstTemp = new List<DateTime>();
            for (DateTime dtTemp = startWeek; dtTemp <= endWeek; dtTemp = dtTemp.AddDays(1))
            {
                char cTemp = Num2Upper(n);
                string strTemp = "星期" + cTemp.ToString() + "\n";
                strTemp += dtTemp.ToShortDateString();
                dt.Columns.Add(strTemp, System.Type.GetType("System.String"));
                dt.Columns[strTemp].SetOrdinal(nIndex++);
                lstTemp.Add(dtTemp);
                n++;
            }

            DataSet ds = m_bllCK_TimeSegTab.GetList("");

            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    string strClassTime = ds.Tables[0].Rows[i]["CK_ClassTime"].ToString();
                    n = 1;
                    DataRow dr = dt.NewRow();
                    dt.Rows.Add(dr);
                    dt.Rows[i]["   "] = strClassTime;
                    foreach (DateTime dtTemp in lstTemp)
                    {
                        char cTemp = Num2Upper(n);
                        string strTemp = "星期" + cTemp.ToString() + "\n";
                        strTemp += dtTemp.ToShortDateString();
                        DataRow[] drArray = dsTemp.Tables[0].Select("时间段 = '" + strClassTime + "' and 上课日期 = '" + dtTemp + "'");
                        
                        if (drArray.Count() > 0)
                        {
                            dt.Rows[i][strTemp] = drArray[0]["课程教师教室"];
                        }
                        else
                        {
                            dt.Rows[i][strTemp] = "";
                        }
                        n++;
                    }
                }
            }

            ultraGrid1.DataSource = null;
            ultraGrid1.DataSource = dt;
        }
        private void dateTimePickerRef_ValueChanged(object sender, EventArgs e)
        {
            ShowClassTable(this.dateTimePickerRef.Value.Date);
        }

        private void ultraGrid1_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            ultraGrid1.DisplayLayout.Override.RowSizing = RowSizing.AutoFree;
            //InitColumnWidth();
            //ClassUltraGridCommonSet.ultraGrid1_InitializeRowFilter(e);
            ClassUltraGridCommonSet.InitUltraWinGridRowNumSetting(e);
            e.Layout.Override.CellAppearance.TextVAlign = Infragistics.Win.VAlign.Middle;
            //e.Layout.Override.DefaultRowHeight = 50;
            for(int i = 1; i < this.ultraGrid1.DisplayLayout.Bands[0].Columns.Count;i++)
            {
                e.Layout.Bands[0].Columns[i].CellMultiLine = DefaultableBoolean.True;
            }
            //e.Layout.Override.CellMultiLine = DefaultableBoolean.True; 
        }

        private void buttonThisWeek_Click(object sender, EventArgs e)
        {
            this.dateTimePickerRef.Value = System.DateTime.Now.Date;
        }

        private void buttonLastWeek_Click(object sender, EventArgs e)
        {
            this.dateTimePickerRef.Value = this.dateTimePickerRef.Value.AddDays(-7);
        }

        private void buttonNextWeek_Click(object sender, EventArgs e)
        {
            this.dateTimePickerRef.Value = this.dateTimePickerRef.Value.AddDays(7);
        }

        private void buttonLastMonth_Click(object sender, EventArgs e)
        {
            this.dateTimePickerRef.Value = this.dateTimePickerRef.Value.AddMonths(-1);
        }

        private void buttonNextMonth_Click(object sender, EventArgs e)
        {
            this.dateTimePickerRef.Value = this.dateTimePickerRef.Value.AddMonths(1);
        }

        private void buttonExportExcel_Click(object sender, EventArgs e)
        {
            
            DataTable dt = (DataTable)ultraGrid1.DataSource;

            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Filter = "Excel文件(*.xls)|*.xls";
                    string outFile = "";

                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        outFile = sfd.FileName;
                    }
                    else
                    {
                        return;
                    }

                    ultraGridExcelExporter1.Export(ultraGrid1, outFile);
                }
            }
        }


    }
}
